package com.buybuyyi.stylistcrm.resource.controllers.home;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import stylistcrm.core.services.security.dto.NavigationDto;
import stylistcrm.core.services.security.dto.UserDto;
import stylistcrm.core.services.security.exception.ReloginException;
import stylistcrm.core.services.security.service.SecurityService;

/**
 * Created by yanwenchuang on 2016/10/18.
 */
@RestController
public class HomeController {
    private Logger logger = LogManager.getLogger(HomeController.class);

    @Autowired
    SecurityService securityService;

    @RequestMapping("/security/navigation")
    public ResponseEntity<NavigationDto> getUserNavigationDto(){
        UserDto userDto = null;
        try {
            userDto = securityService.getCurrentUser();
            if(userDto == null){
                logger.warn("需要重新登录系统");
                return new ResponseEntity<NavigationDto>(new NavigationDto(),HttpStatus.UNAUTHORIZED);
            }
        } catch (ReloginException e) {
            logger.warn("需要重新登录系统");
            e.printStackTrace();
            return new ResponseEntity<NavigationDto>(new NavigationDto(),HttpStatus.UNAUTHORIZED);
        }catch(Exception e){
            e.printStackTrace();
            return new ResponseEntity<NavigationDto>(new NavigationDto(),HttpStatus.UNAUTHORIZED);
        }
        return new ResponseEntity<NavigationDto>(userDto.getNavigation(), HttpStatus.OK);
    }
}
